// pages/videoDetail/videoDetail.js
var businessLogic = require('../../../utils/NetWork/Model/VideobusinessLogic.js')
var common = require('../../../utils/common.js')
var dataUrl = require('../../../utils/NetWork/sysJson.js')
var uuid = require('../../../utils/Component/node-uuid/uuidnew.js');
var isCollected = new Array()
var videoExam = new Array()
var aaa = [];
let asign = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    video: [],
    collectid: [],
    tancontent: '现在有一道题目需要完成',
    hhh: 1111,
    stime: '',
    orgid: '',
    userid: '',
    videoid: '',
    parid: '',
    isFlod: '',
    isFoldTxt: '',
    passscore: '',
    type: '',
    checkCollection: '',
    examid: '',
    checkpointid: '',
    exam: '',
    videopath:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var myDate = Date.parse(new Date());
    console.log(myDate)
    let c = common.Getorgid();
    this.setData({
      content: '',
      stime: myDate,
      orgid: c.orgid,
      userid: c.userid,
      videoid: options.id
    });
    //console.log(uuid.uuid()); 
    this.videoInformation();
    this.QueryVideoCollection();
   // this.VideoLevelList();
    // this.QueryVideoLevelList(videoid);
    this.VideoAddOne();

  },
  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   aaa = [];
    asign=0;
    this.VideoLevelList(); 
    var myDate = Date.parse(new Date());
    this.setData({
      //endtime: '',
      stime: myDate,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    isCollected.splice(0, isCollected.length)
    videoid: '';
    this.AddPeVideo();
    this.setData({
      //endtime: '',
      stime: '',
    })
    console.log(this.data.stime)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //
  isFlod: function () {
    this.setData({
      isFlod: !this.data.isFlod,
      isFoldTxt: !this.data.isFoldTxt
    })
  },
  record: function (e) {
    this.AddPeVideo();
    var url = '../videoRecord/videoRecord?videoid=' + this.data.videoid
    wx.navigateTo({
      url: url,
    })
    wx.createVideoContext('ee').pause();//停止播放

  },
  bindplay: function () {//开始播放按钮或者继续播放函数
    console.log("开始播放");
    /*  var myDate = Date.parse(new Date());
     this.setData({
       stime: myDate,
      }) */
  },
  bindpause: function () {//暂停播放按钮函数
    console.log("停止播放")
  },
  bindend: function () {//播放结束按钮函数
    console.log("播放结束")
  },
  bindtimeupdate: function (res) {//播放中函数，查看当前播放时间等
    //console.log("播放到第" + res.detail.currentTime + "秒")//查看正在播放时间，以秒为单位
    for (var i = 0; i <= videoExam[0].length - 1; i++) {
      if (res.detail.currentTime.toFixed(0) == videoExam[0][i].stime) {
        this.setData({
          parid: videoExam[0][i].examid,
          type: videoExam[0][i].type,
          passscore: videoExam[0][i].passscore,
        })
        var guanka = videoExam[0][i].id;
        //5.12查询该关卡是否已经通过
        let params = { 'page': 1, 'num': 1000, "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "videoid,peopleid, checkpointid", "searchValue": this.data.videoid + "," + this.data.userid + "," + videoExam[0][i].id }
        businessLogic.VideoPass(params, (res1) => {
          console.log("5.12", res1)
          if (res1.ispass == 0 || res1.length == 0) {
            let paramss = { "userid": this.data.userid, "orgid": this.data.orgid, "searchField": "peopleid,examtype,statemark", "searchValue": "" + this.data.userid + ",0,0" }
            businessLogic.QueryVideoPass(paramss, (res2) => {
              console.log("5.13", res2)
              if (res2 == '' || res2 == undefined) {
                var exam = uuid.uuid();
                console.log(exam)
                this.setData({
                  examid: exam,
                  checkpointid: guanka,
                })
                console.log("exan", this.data.examid)
                console.log("paper", this.data.parid)
                this.tan(exam);
                wx.createVideoContext('ee').pause();//停止播放
              }
              else {
                let paramsm = { "userid": this.data.userid, "orgid": this.data.orgid, "id": guanka }
                businessLogic.Videoguanka(paramsm, (resm) => {
                  console.log("5.14", resm)
                  this.setData({
                    checkpointid: guanka,
                    parid: resm[0].examid,
                    examid: res2[0].examid,
                    videoid: resm[0].videoid,
                  })
                  this.tan();
                  wx.createVideoContext('ee').pause();//停止播放
                })
              }
            })
          }
        })
      }
    }
  },
  tan: function (exam) {
    this.modalcnttiao("播放提示", this.data.tancontent, "继续播放", "现在去做");
  },

  //选择弹窗
  modalcnttiao: function (title, content, cancelText, confirmText, ) {
    // console.log(tiaozhuan);
    var that = this;

    wx.showModal({
      title: title,
      content: content,
      cancelText: cancelText,
      confirmText: confirmText,
      success: function (res) {
        if (res.confirm) {
          that.tiao();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  tiao: function () {
    wx.navigateTo({
      url: "../../Main/answeron/answeron?remarktype=0&&examtype=0&&examid=" + this.data.examid + "&&parid=" + this.data.parid + "&&video=" + this.data.videoid + "&&checkpointid=" + this.data.checkpointid + "&&type=" + this.data.type + "&&passscore=" + this.data.passscore
    });
  },
  // 5.4查询视频内容
  videoInformation: function () {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, id: this.data.videoid }
    businessLogic.VideoContent(params, (res) => {
      //console.log("click", res.onclick)
      if (res.onclick > 999999) {
        res.onclick = 999999 + "+";
      } else {
        res.onclick = res.onclick + "次";
      }
      let videoparams = { "fileid": res.path }
      businessLogic.findFileInfo(videoparams, (res2) => {
        console.log(res2)
        if (res2.filepath.indexOf("upload") > -1) {
         var aaaa = res2.filepath.replace(/\\/g, "/");
          var uri = dataUrl.dataUrl2 + "upload" + aaaa.split("upload")[1];
          console.log("wwwwwww1=" + uri)
          this.setData({
            videopath: uri
          })
        }

      })
      this.setData({
        video: res
      })
      var addone = res.onclick
      //this.VideoAddOne(addone) //点击量加一
    });
  },
  QueryVideoCollection: function () {
    // 5.5.2查询视频是否收藏（已测试）
    let params = { orgid: this.data.orgid, userid: this.data.userid, searchField: "peopleid, videoid", searchValue: this.data.userid + "," + this.data.videoid }
    businessLogic.QueryVideoCollection(params, (res) => {
      if (res) {
        isCollected.push(res);
        this.setData({ checkCollection: isCollected[0].iscollect })
      } else {
        this.setData({ checkCollection: 0 })
      }
    });
  },
  Collection: function () {
    if (!isCollected[0]) {
      this.firstCollect()
    } else if (isCollected[0].iscollect == "0") {
      var collectid = isCollected[0].id
      this.recollect(collectid)
    } else {
      var collectid = isCollected[0].id
      this.cancelCollect(collectid)
    }
  },

  recollect: function (collectid) {
    //再次收藏
    var InfoJson = "{orgid:'" + this.data.orgid + "',userid:'" + this.data.userid + "',peopleid:'" + this.data.userid + "',videoid:'" + this.data.videoid + "',id:'" + collectid + "',iscollect:1}"
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "InfoJson": InfoJson }
    businessLogic.VideoCollection(params, (res) => {
      this.setData({ checkCollection: 1 })
      isCollected[0].iscollect = "1"
    });
  },

  firstCollect: function (collectid) {
    //首次收藏
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "InfoJson": "{orgid:'" + this.data.orgid + "',userid:'" + this.data.userid + "',peopleid:'" + this.data.userid + "',videoid:'" + this.data.videoid + "',id:'',iscollect:1}" }
    businessLogic.VideoCollection(params, (res) => {
      this.setData({ checkCollection: 1 })
      isCollected.push(res)
    });
  },

  cancelCollect: function (collectid) {
    //取消收藏
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "InfoJson": "{orgid:'" + this.data.orgid + "',userid:'" + this.data.userid + "',peopleid:'" + this.data.userid + "',videoid:'" + this.data.videoid + "',id:'" + collectid + "',iscollect:0}" }
    businessLogic.VideoCollection(params, (res) => {
      this.setData({ checkCollection: 0 })
      isCollected[0].iscollect = '0'
    });
  },
  //5.11视频点击数加一
  VideoAddOne: function () {
    //播放量加1
    let params = { "id": this.data.videoid }
    businessLogic.VideoAddOne(params, (res) => {
      // console.log("5.11",res)
    });
  },
  //5.9添加人员视频记录
  AddPeVideo: function (addone) {
    var endtime = Date.parse(new Date());
    //console.log(myDate)
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "InfoJson": "{orgid:'" + this.data.orgid + "',classid:'f9257749-afc3-4412-a99f-33f081cd7141',userid:'" + this.data.userid + "',peopleid:'" + this.data.userid + "',videoid:'" + this.data.videoid + "',stime:'" + this.data.stime + "',etime:'" + endtime + "',type:0,score:''}" }
    businessLogic.AddPeVideo(params, (res) => {
      // console.log("5.9",res)
    });
  },
  //5.6查询视频关卡列表
  VideoLevelList: function () {
    //视频节点
    aaa = [];
    let params = { page: 1, num: 1000, orgid: this.data.orgid, userid: this.data.userid, searchField: "videoid", searchValue: this.data.videoid }
    businessLogic.VideoLevelList(params, (res) => {
      console.log("5.6", res)
      this.setData({
        exam: res
      })
      for (var i = 0; i < this.data.exam.length; i++) {
        aaa[aaa.length] = this.data.exam[i];
        if (i == (this.data.exam.length - 1)) {
          this.getdata(aaa.length, this.data, res)
        }
      }
      videoExam.push(res)
      // console.log("222",videoExam[0][0])
    });
  },
  //5.7查询个人视频关卡考试记录列表
  getdata: function (alength, data, res) {
    if (asign == alength) {
      asign = 0;
      aaa = [];
    } else {
      let params2 = { page: 1, num: 1000, orgid: this.data.orgid, userid: this.data.userid, searchField: "videoid,peopleid,checkpointid", searchValue: this.data.videoid + "," + this.data.userid + "," + res[asign].id }
      businessLogic.QueryVideoLevelList(params2, (res2) => {
        //console.log("gg", res2[0].time)
        if (res2.length == 0) {

        } else {
          if (res2[0].time == "" || res2[0].time == undefined) {
            res2[0].time = '暂无';
          } else {
            res2[0].time = common.formatSeconds(res2[0].time);
          }
        }
        aaa[asign] = res2;
        //console.log("5.7", aaa)
        this.setData({
          content: aaa
        })
        //console.log(this.data.content)
        asign += 1;
        this.getdata(alength, data, res);
      })
    }
  }
})